 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-9-10  <br>
 */
package com.ce.pms.charge.service.impl;

import java.util.Set;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.IAccountDao;
import com.ce.pms.charge.dao.IPrePaySchDao;
import com.ce.pms.charge.dao.ISubAccountDao;
import com.ce.pms.charge.model.AbstractSubAccount;
import com.ce.pms.charge.model.AccountDomain;
import com.ce.pms.charge.model.ChargeAccountDomain;
import com.ce.pms.charge.model.DepositAccountDO;
import com.ce.pms.charge.model.PrePayAccountDomain;
import com.ce.pms.charge.model.PrePaySchDomain;
import com.ce.pms.charge.service.ISubAccountService;
import com.ce.pms.charge.vo.AbstractSubAccountVO;
import com.ce.pms.charge.vo.ChargeAccountVO;
import com.ce.pms.charge.vo.DepositAccountVO;
import com.ce.pms.charge.vo.PrePayAccountVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;

/**
 * 	名称: com.ce.pms.charge.service.impl.SubAccountServiceImpl <br>
 *
 *
 * @author  付湘辉  <br>
 * Created on： 2008-9-10 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class SubAccountServiceImpl extends PmsBaseServiceImpl implements
		ISubAccountService {
	protected IPrePaySchDao  prePaySchDao;

	protected ISubAccountDao subAccountDao;

	protected IAccountDao accountDao;

	public void setAccountDao(IAccountDao accountDao) {
		this.accountDao = accountDao;
	}

	/**
	 * 功能描述: 判断指定的预交款账户是否选择了预交方案 <br>
	 * @param perPayAccountId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean checkIsChoosePerPaySch(Long perPayAccountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", perPayAccountId);
		AbstractSubAccount subAccount = this.subAccountDao.findById(perPayAccountId);
		if (subAccount instanceof PrePayAccountDomain) {
			PrePayAccountDomain prePayAcc = (PrePayAccountDomain)subAccount;
			return prePayAcc.getPrePaySch() != null;
		}
		return false;
	}

	public void setPrePaySchDao(IPrePaySchDao prePaySchDao) {
		this.prePaySchDao = prePaySchDao;
	}

	/**
	 * 功能描述: 给指定的预交款账户设定预交方案 <br>
	 * @param perPayAccountId 预交款账户ID
	 * @param perPaySchId 预交方案ID
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public PrePayAccountVO setPrePayScheme(Long perPayAccountId,Long perPaySchId){
		super.checkParameterIsNull("common:parameters_null_exception, ", perPayAccountId,perPaySchId);
		AbstractSubAccount subAccount = this.subAccountDao.findById(perPayAccountId);
		if (subAccount instanceof PrePayAccountDomain) {
			PrePayAccountDomain prePayAcc = (PrePayAccountDomain)subAccount;
			PrePaySchDomain prePaySch = this.prePaySchDao.findById(perPaySchId);
			prePayAcc.setPrePaySch(prePaySch);
			return (PrePayAccountVO)this.assembleD2V(subAccount);
		}
		return null;
	}

	/**
	 * 功能描述: 查询指定的总账户的预交款子账户 <br>
	 * @param accountId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public PrePayAccountVO getPrePayAccountById(Long accountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", accountId);
		AccountDomain account = this.accountDao.findById(accountId);
		Set<AbstractSubAccount> set = account.getSubAccounts();
		for (AbstractSubAccount subAccount : set) {
			if (subAccount instanceof PrePayAccountDomain) {
				return (PrePayAccountVO)this.assembleD2V(subAccount);
			}
		}
		return null;
	}

	public void setSubAccountDao(ISubAccountDao subAccountDao) {
		this.subAccountDao = subAccountDao;
	}

	/**
	 * 功能描述: 锁定借方 <br>
	 * @param subAccountId 子账户id
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-20  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void lockDebit(Long subAccountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", subAccountId);
		AbstractSubAccount domain = this.subAccountDao.findById(subAccountId);
		//得到原锁定状态
		String oldLockState = domain.getLockState();
		char creditChar = '0';
		//取出贷方锁定值
		creditChar = oldLockState.charAt(SysrefcodeDO.CONSTANTS_ONE);

		//设置新锁定状态
		String lockState = SysrefcodeDO.CONSTANTS_DIGITAL_ONE + creditChar;
		this.subAccountDao.updateLockState(subAccountId, lockState);
	}

	/**
	 * 功能描述: 锁定贷方 <br>
	 * @param subAccountId 子账户id
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-20  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void lockCredit(Long subAccountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", subAccountId);
		AbstractSubAccount domain = this.subAccountDao.findById(subAccountId);
		//得到原锁定状态
		String oldLockState = domain.getLockState();
		char debitChar = '0';
		//取出借方锁定值
		debitChar = oldLockState.charAt(SysrefcodeDO.CONSTANTS_ZERO);
		//设置新锁定状态
		String lockState = debitChar + SysrefcodeDO.CONSTANTS_DIGITAL_ONE;
		this.subAccountDao.updateLockState(subAccountId, lockState);
	}

	/**
	 * 功能描述: 锁定借方和贷方：11 <br>
	 * @param subAccountId 子账户id
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-20  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void lockDC(Long subAccountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", subAccountId);
		//设置完全锁定状态：11
		String lockState = SysrefcodeDO.FEE_ACCOUNT_LOCK_STATE_LOCK_ALL;
		this.subAccountDao.updateLockState(subAccountId, lockState);
	}

	/**
	 * 功能描述: 置账户借方正常：0? <br>
	 * @param subAccountId 子账户id
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-20  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void unblockDebit(Long subAccountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", subAccountId);
		AbstractSubAccount domain = this.subAccountDao.findById(subAccountId);
		//得到原锁定状态
		String oldLockState = domain.getLockState();

		char creditChar = '0';

		// 取出贷方锁定值
		creditChar = oldLockState.charAt(SysrefcodeDO.CONSTANTS_ONE);
		//设置新锁定状态
		String lockState = SysrefcodeDO.CONSTANTS_DIGITAL_ZERO + creditChar;
		this.subAccountDao.updateLockState(subAccountId, lockState);
	}

	/**
	 * 功能描述: 置账户贷方正常：?0 <br>
	 * @param subAccountId 子账户id
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-20  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void unblockCredit(Long subAccountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", subAccountId);
		AbstractSubAccount domain = this.subAccountDao.findById(subAccountId);
		//得到原锁定状态
		String oldLockState = domain.getLockState();
		char debitChar = '0';
		//取出借方锁定值
		debitChar = oldLockState.charAt(SysrefcodeDO.CONSTANTS_ZERO);
		//设置新锁定状态
		String lockState = debitChar + SysrefcodeDO.CONSTANTS_DIGITAL_ZERO;
		this.subAccountDao.updateLockState(subAccountId, lockState);
	}

	/**
	 * 功能描述: 置账户锁定状为完全正常：00 <br>
	 * @param subAccountId 子账户id
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-20  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void unblockDC(Long subAccountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", subAccountId);
		//设置完全正常状态：00
		String lockState = SysrefcodeDO.FEE_ACCOUNT_LOCK_STATE_NOLOCK;
		this.subAccountDao.updateLockState(subAccountId, lockState);
	}

	/**
	 * 功能描述: 查看是否锁定了账户借方 <br>
	 * @param subAccountId 子账户id
	 * @return true 表示锁定
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-20  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean isLockDebit(Long subAccountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", subAccountId);
		AbstractSubAccount domain = this.subAccountDao.findById(subAccountId);
		//得到原锁定状态
		String oldLockState = domain.getLockState();
		char debitChar = '0';
		// 取出借方锁定值
		debitChar = oldLockState.charAt(SysrefcodeDO.CONSTANTS_ZERO);
		return SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(String.valueOf(debitChar));
	}

	/**
	 * 功能描述: 查看是否锁定了账户贷方 <br>
	 * @param subAccountId 子账户id
	 * @return true 表示锁定
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-10-20  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Boolean isLockCredit(Long subAccountId){
		super.checkParameterIsNull("common:parameters_null_exception, ", subAccountId);
		AbstractSubAccount domain = this.subAccountDao.findById(subAccountId);

		//得到原锁定状态
		String oldLockState = domain.getLockState();
		char creditChar = '0';
		// 取出贷方锁定值
		creditChar = oldLockState.charAt(SysrefcodeDO.CONSTANTS_ONE);
		return SysrefcodeDO.CONSTANTS_DIGITAL_ONE.equals(String.valueOf(creditChar));
	}

	/**
	 * 功能描述: 装配一个子账户VO,若要得到一个具体的VO，则需要进行转换 <br>
	 * @param subAccount
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-9-10  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private AbstractSubAccountVO assembleD2V(AbstractSubAccount subAccount){
		AbstractSubAccountVO subAccountVo = null;
		if (!BlankUtil.isBlank(subAccount)) {
			if (subAccount instanceof PrePayAccountDomain) {
				subAccountVo = new PrePayAccountVO();
				PrePayAccountVO prePayAccountVo = (PrePayAccountVO)subAccountVo;
				PrePaySchDomain prePaySch = ((PrePayAccountDomain)subAccount).getPrePaySch();
				if (!BlankUtil.isBlank(prePaySch)) {
					prePayAccountVo.setPrePaySchId(prePaySch.getPrePaySchId());
					prePayAccountVo.setSchemeName(prePaySch.getSchemeName());
				}

			} else if (subAccount instanceof ChargeAccountDomain) {
				subAccountVo = new ChargeAccountVO();
			} else if (subAccount instanceof DepositAccountDO) {
				subAccountVo = new DepositAccountVO();
			}

			BeanUtil.convertVoAsDo(subAccount, subAccountVo);

			AccountDomain account = subAccount.getAccount();
			subAccountVo.setAccountId(account.getAccountId());
		}
		return subAccountVo;
	}
}
